﻿////Copyright 2011 - Jonathan Allen

//using System;
//using System.Linq;
//using System.Windows;
//using System.Windows.Interactivity;
//using System.Windows.Input;
//using System.Diagnostics.Contracts;

//namespace Granite.Xaml.Behaviors
//{

//	/// <summary>
//	/// This behavior fires the indicated ICommand when the user clicks on the control. The trigger is on the MouseUp.
//	/// </summary>
//	public sealed partial class ClickCommandBehavior : Behavior<UIElement>
//	{
//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//		public static readonly DependencyProperty CommandProperty =
//							DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(ClickCommandBehavior), new PropertyMetadata(null, OnCommandChanged));

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public static readonly DependencyProperty CommandParameterProperty =
//					DependencyProperty.Register("CommandParameter", typeof(object), typeof(ClickCommandBehavior), new PropertyMetadata(null));

//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//		public ICommand Command
//		{
//			get
//			{
//				Contract.Assume(AssociatedObject != null);
//				return (ICommand)AssociatedObject.GetValue(CommandProperty);
//			}
//			set
//			{
//				Contract.Assume(AssociatedObject != null);
//				AssociatedObject.SetValue(CommandProperty, value);
//			}
//		}

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public object CommandParameter
//		{
//			get
//			{
//				Contract.Assume(AssociatedObject != null);
//				return (object)AssociatedObject.GetValue(CommandParameterProperty);
//			}
//			set
//			{
//				Contract.Assume(AssociatedObject != null);
//				AssociatedObject.SetValue(CommandParameterProperty, value);
//			}
//		}

//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//		public static ICommand GetCommand(UIElement control)
//		{
//			if (control == null)
//				throw new ArgumentNullException("control", "control is null.");

//			return (ICommand)control.GetValue(CommandProperty);
//		}

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public static object GetCommandParameter(UIElement control)
//		{
//			if (control == null)
//				throw new ArgumentNullException("control", "control is null.");

//			return control.GetValue(CommandParameterProperty);
//		}

//		/// <summary>
//		/// Command that will be executed
//		/// </summary>
//		public static void SetCommand(UIElement control, ICommand value)
//		{
//			if (control == null)
//				throw new ArgumentNullException("control", "control is null.");

//			control.SetValue(CommandProperty, value);
//		}

//		/// <summary>
//		/// Parameter for the command that will be executed
//		/// </summary>
//		public static void SetCommandParameter(UIElement control, object value)
//		{
//			if (control == null)
//				throw new ArgumentNullException("control", "control is null.");

//			control.SetValue(CommandParameterProperty, value);
//		}

//		void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs mouseEventArgs)
//		{
//			if (Command.CanExecute(CommandParameter))
//				Command.Execute(CommandParameter);
//		}

//		static void OnCommandChanged(DependencyObject control, DependencyPropertyChangedEventArgs e)
//		{
//			var behaviors = Interaction.GetBehaviors(control);
//			var existingBehavior = behaviors.OfType<ClickCommandBehavior>().FirstOrDefault();

//			if (e.NewValue == null)
//			{
//				if (existingBehavior != null)
//					behaviors.Remove(existingBehavior);
//			}
//			else
//				behaviors.Add(new ClickCommandBehavior());
//		}

//	}

//#if SILVERLIGHT
//    public partial class ClickCommandBehavior : Behavior<UIElement>
//    {
//		/// <summary>
//		/// 
//		///             Called after the behavior is attached to an AssociatedObject.
//		///             
//		/// </summary>
//		/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
//        protected override void OnAttached()
//        {
//			Contract.Assume(AssociatedObject != null);
//            AssociatedObject.MouseLeftButtonUp += AssociatedObject_MouseUp;
//            AssociatedObject.MouseRightButtonUp += AssociatedObject_MouseUp;
//        }

//		/// <summary>
//		/// 
//		///             Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
//		///             
//		/// </summary>
//		/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
//        protected override void OnDetaching()
//        {
//			Contract.Assume(AssociatedObject != null);
//            AssociatedObject.MouseLeftButtonUp -= AssociatedObject_MouseUp;
//            AssociatedObject.MouseRightButtonUp -= AssociatedObject_MouseUp;
//        }
//    }
//#endif

//#if WPF
//	public partial class ClickCommandBehavior : Behavior<UIElement>
//	{
//		/// <summary>
//		/// Called after the behavior is attached to an AssociatedObject.
//		/// </summary>
//		/// <remarks>Override this to hook up functionality to the AssociatedObject.</remarks>
//		protected override void OnAttached()
//		{
//			Contract.Assume(AssociatedObject != null);
//			AssociatedObject.MouseUp += AssociatedObject_MouseUp;
//		}

//		/// <summary>
//		/// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
//		/// </summary>
//		/// <remarks>Override this to unhook functionality from the AssociatedObject.</remarks>
//		protected override void OnDetaching()
//		{
//			Contract.Assume(AssociatedObject != null);
//			AssociatedObject.MouseUp -= AssociatedObject_MouseUp;
//		}

//	}
//#endif

//}